Violin Expression Plots
IndividualGenes_Violins(data = corrcounts_merge, metadata = metadata_merge, genes = c("CDKN1A", "CDKN2A", "GLB1","TP53","CCL2"), GroupingVariable = "Condition", plot=T, ncol=NULL, nrow=2, divide="CellType", invert_divide=FALSE,ColorValues=senescence_triggers_colors, pointSize=2, ColorVariable="SenescentType", title="Senescence", widthTitle=16,y_limits = NULL,legend_nrow=4, xlab="Condition",colorlab="")
Using gene as id variables

Correlation Heatmap
CorrelationHeatmap(data=corrcounts_merge,
metadata = metadata_merge,
genes=c("CDKN1A", "CDKN2A", "GLB1","TP53","CCL2"),
separate.by = "Condition",
method = "pearson",
colorlist = list(low = "#3F4193", mid = "#F9F4AE", high = "#B44141"),
limits_colorscale = c(-1,0,1),
widthTitle = 16,
title = "test",
cluster_rows = TRUE,
cluster_columns = TRUE,
detailedresults = FALSE,
legend_position="right",
titlesize=20)
Warning: Heatmap/annotation names are duplicated: pearson's coefficient
Warning: Heatmap/annotation names are duplicated: pearson's coefficient, pearson's coefficient
Warning: `legend_height` you specified is too small, use the default minimal height.
Warning: `legend_height` you specified is too small, use the default minimal height.
Warning: `legend_height` you specified is too small, use the default minimal height.

PCA with genes from signature only

#' @importFrom edgeR DGEList
#' @importFrom stats prcomp
#' @import ggplot2
#' @importFrom ggpubr ggarrange
plotPCA <- function(data, metadata, genes=NULL, scale=FALSE, center=TRUE, PCs=list(c(1,2)), ColorVariable=NULL,ColorValues=NULL,pointSize=5,legend_nrow=2, legend_position=c("bottom","top","right","left"),ncol=NULL, nrow=NULL){
legend_position <- match.arg(legend_position)
if (is.null(genes)){
genes <- row.names(data)
}
data <- data[row.names(data) %in% genes, , drop=F]
if (!nrow(data)>1) stop(paste0("Error: Number of genes should be >1; In your data you have only found the gene ",genes))
# Ensure metadata matches sample order if provided
if (!is.null(metadata)) {
colnames(metadata)[1] <- "Sample"
rownames(metadata) <- metadata$Sample
metadata <- metadata[colnames(data), , drop = FALSE]
y <- edgeR::DGEList( log2(data+1), samples= metadata)
} else {
y <- edgeR::DGEList( log2(data+1))
}
nPCs <- max(unlist(PCs)) # get the maximum number of PC based on the user's choice
PCAdata <- stats::prcomp(t(y$counts), scale=scale, center=center)
PCAcounts <- PCAdata$x
PCAcounts <- as.data.frame(PCAcounts)
if (nPCs > ncol(PCAcounts)) stop("Error: Number of genes too low for number of chosen PCs. Please reduce number of PCs.")
PCAcounts <- cbind(PCAcounts[,1:nPCs],y$samples)
pltList <- list()
for (pc in PCs){
pc <- unlist(pc)
ev = PCAdata$sdev^2
pc_x <- round(100*ev[pc[1]]/sum(ev),2)
pc_y <- round(100*ev[pc[2]]/sum(ev),2)
plt <- ggplot2::ggplot(PCAcounts, ggplot2::aes_string(y = paste0("PC",pc[1]), x = paste0("PC",pc[2])))
# Add jittered points, optionally colored by ColorVariable.Default: Brewer Pallette "Paired"
if (!is.null(ColorVariable)) {
plt <- plt + ggplot2::geom_point(ggplot2::aes_string(fill = ColorVariable), size = pointSize, alpha = 0.5, shape=21, color="black")
} else {
plt <- plt + ggplot2::geom_point(size = pointSize, alpha = 0.5, shape=21, color="black", fill="#D8D8D8")
}
# If ColorValues is provided, use a manual color scale; otherwise, if ColorVariable is provided,
# use a default brewer palette.
if (!is.null(ColorValues)) {
plt <- plt + ggplot2::scale_fill_manual(values = ColorValues)
} else if (!is.null(ColorVariable)) {
plt <- plt + ggplot2::scale_fill_brewer(palette = "Paired")
}
# Add axis labels (including variance)
xlab <- paste0("PC",pc[1],": ",pc_x,"% variance")
ylab <- paste0("PC",pc[2],": ",pc_y,"% variance")
titleplot <- paste0("PC",pc[1], " vs PC",pc[2])
plt <- plt + ggplot2::labs(fill = "", x = xlab, y = ylab, title=titleplot)
# Change theme
plt <- plt +
ggplot2::theme_bw()+
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust=1),
plot.title = ggplot2::element_text(hjust = 0.5),
legend.position = "bottom")
# Adjust legend rows if legend_nrow is specified
if (!is.null(legend_nrow)) {
plt <- plt + ggplot2::guides(fill = ggplot2::guide_legend(nrow = legend_nrow, position = legend_position))
}
# Add reference lines
plt <- plt +
ggplot2::geom_vline(xintercept=0, linetype="dotted") +
ggplot2::geom_hline(yintercept=0, linetype="dotted")
pltList <- c(pltList, list(plt))
}
n <- length(pltList)
if(n==1){
plt <- pltList[[1]]
} else {
# Determine grid layout
if (is.null(ncol) && is.null(nrow)) {
ncol <- ceiling(sqrt(n))
nrow <- ceiling(n / ncol)
} else if (is.null(ncol)){
ncol <- ceiling(n / nrow)
} else if (is.null(nrow)){
nrow <- ceiling(n / ncol)
}
if (!is.null(ColorVariable)) {
plt <- ggpubr::ggarrange(plotlist = pltList, ncol = ncol, nrow = nrow, common.legend = TRUE, align = "h")
} else {
plt <- ggpubr::ggarrange(plotlist = pltList, ncol = ncol, nrow = nrow, align = "h")
}
}
print(plt)
invisible(list(plt = plt,
data = PCAcounts))
}